<template>
  <view class="category">
    <PageSearch
      v-model="keyword"
      @change="$u.throttle(resetList, 500, false)"
      :disabled="false"
    ></PageSearch>
  </view>
  <view class="sort">
    <view class="kinds" @click="show = true">
      <image class="img" src="@/static/tabBar/category.png"></image>
    </view>
    <u-scroll-list :indicator="false">
      <view
        v-for="item in list"
        :key="item.id"
        class="item"
        :class="currentList === item.id && 'active'"
        @click="() => handleChange(item.id)"
      >
        <view class="img">
          <image class="icon" :src="item.categoryImage"></image>
        </view>
        <view class="text u-line-1">{{ item.categoryName }}</view>
      </view>
    </u-scroll-list>
  </view>
  <view
    class="box"
    :style="{
      height: listHeight.height,
    }"
  >
    <treeSelect
      ref="treeSelectRef"
      :height="listHeight.height"
      @edit="handleEdit"
      :leftData="listTwo"
      :rightData="getRightData"
    >
      <template #default="data">
        <div class="orderItem" v-for="item in data.list">
          <view class="left">
            <image class="img" :src="item.goodsImage"></image>
          </view>
          <view class="right">
            <view class="name">{{ item.goodsName }}</view>
            <view class="desc">规格: {{ item.skuName || "默认规格" }}</view>
            <view class="desc"
              >库存数量: {{ item.invNum }}{{ item.unitName }}</view
            >
          </view>
          <view class="state" @click="() => handleDetail(item)">明细</view>
        </div>
      </template>
    </treeSelect>
  </view>
  <u-popup :show="show" mode="top" @close="show = false">
    <view class="category">
      <view class="sortPopup">
        <view
          v-for="item in list"
          :key="item.id"
          class="item"
          :class="currentList === item.id && 'active'"
          @click="() => handleChange(item.id)"
        >
          <view class="img">
            <image class="icon" :src="item.categoryImage"></image>
          </view>
          <view class="text u-line-1">{{ item.categoryName }}</view>
        </view>
      </view>
    </view>
  </u-popup>
</template>

<script setup>
import PageSearch from "@/components/search/pageSearch.vue";
import { computed, ref } from "vue";
import { useListHeight } from "@/hooks/useListHeight.js";
import { onShow } from "@dcloudio/uni-app";
import { getCategoryList, getCategoryPage } from "@/api/order";

onShow(() => {
  getMenuOne();
});
const { listHeight } = useListHeight({
  el: ".box",
  hasTabBar: true,
  hasNav: true,
});
// 一级菜单
const list = ref([]);
const listTwo = computed(() => {
  try {
    return list.value.filter((btn) => btn.id === currentList.value)[0].child;
  } catch (e) {
    console.log(e);
    return [];
  }
});
// 当前选中的一级菜单
const currentList = ref("");
const getMenuOne = async () => {
  const res = await getCategoryList();
  list.value = res;
  if (res[0]) {
    currentList.value = res[0].id;
  }
};

const handleChange = (index) => {
  currentList.value = index;
};
const keyword = ref();
const getRightData = async (params) => {
  const res = await getCategoryPage({
    goodsName: keyword.value,
    page: params.page,
    limit: params.limit,
    categoryId: params.id,
  });
  return res;
};

const show = ref(false);

const editRef = ref();
const handleEdit = (data) => {
  editRef.value?.open(data);
};

const handleDetail = (item) => {
  uni.navigateTo({
    url: `/pages/orderList/repertory/repertoryDetail?productSkuId=${item.productSkuId}&unitId=${item.unitId}`,
  });
};
const treeSelectRef = ref();
const resetList = () => {
  treeSelectRef.value.resetList();
};
</script>

<style lang="scss">
.sort {
  padding: 2px 12px 2px;
  position: relative;

  .kinds {
    position: absolute;
    right: 15px;
    top: 0;
    height: 43px;
    width: 24px;
    box-shadow: -10px 0px 10px #ccc;
    background-color: #fff;
    z-index: 1000;
    border-radius: 4px;

    .img {
      display: block;
      width: 12px;
      height: 12px;
      margin-top: 14px;
      margin-left: 5px;
    }
  }
}

.item {
  min-width: 66px;

  .img {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    overflow: hidden;
    border: 1px solid transparent;
    box-sizing: border-box;
    margin: 0 auto 15px;

    .icon {
      display: block;
      width: 100%;
      height: 100%;
    }
  }

  .text {
    color: #333333;
    font-size: 13px;
    text-align: center;
    width: 66px;
  }

  &.active {
    .img {
      border-color: #ff6105;
    }

    .text {
      font-weight: 500;
      color: #ff6105;
    }
  }
}

.sortPopup {
  display: flex;
  flex-wrap: wrap;
  padding: 2px 15px 28px;
  max-height: 300px;
  overflow-y: auto;

  .item {
    margin-bottom: 10px;

    .img {
      margin: 0 auto 15px;
    }

    .text {
      text-align: center;
    }
  }
}

.orderItem {
  display: flex;
  position: relative;
  padding: 12px 4px;
  background-color: #fff;
  margin: 0 15px;

  .left {
    margin-right: 10px;

    .img {
      display: block;
      width: 55px;
      height: 50px;
    }
  }

  .right {
    .name {
      line-height: 20px;
      font-size: 14px;
      color: #333;
      margin-bottom: 2px;
    }

    .desc {
      color: #a0a0a0;
      font-size: 10px;
      line-height: 15px;
      margin-bottom: 2px;
    }
  }

  .state {
    position: absolute;
    right: 9px;
    bottom: 13px;
    height: 20px;
    width: 50px;
    line-height: 20px;
    border-radius: 12px;
    border: 1px solid #fd4d00;
    background-color: #fff;
    color: #fd4d00;
    font-size: 10px;
    text-align: center;
  }
}
</style>
